package com.redis.controller;

import org.redisson.RedissonRedLock;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author CKC
 * @DateTime 2021/11/13 8:23
 */
@RestController
public class GoodController {
    public static final String Redis_Lock = "hello";

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Value("${server.port}")
    private String serverPort;

    @Resource
    private RedissonClient redisson;


    @GetMapping("/buy_goods")
    public String buy_Goods() throws InterruptedException {
        //stringRedisTemplate.opsForValue().set("goods:001", "100");
        RLock redissonLock = redisson.getLock(Redis_Lock);//非公平锁  Redis_Lock = "hello";
        redissonLock.lock();
        try {
            //get key  ======看看库存的数量够不够
            String result = stringRedisTemplate.opsForValue().get("goods:001");

            int goodsNumber = result == null ? 0 : Integer.parseInt(result);

            if (goodsNumber > 0) {
                int realNumber = goodsNumber - 1;
                stringRedisTemplate.opsForValue().set("goods:001", String.valueOf(realNumber));
                System.out.println("成功买到商品，库存还剩下：  " + realNumber + "件\t服务端口号： " + serverPort);

                return "成功买到商品，库存还剩下：  " + realNumber + "件\t服务端口号： " + serverPort;
            } else {
                System.out.println("商品已经售完、活动结束、调用超时......  端口号：  " + serverPort);
            }
            return "商品已经售完、活动结束、调用超时......  端口号：  " + serverPort;
        } finally {
            if (redissonLock.isLocked()) {
                if (redissonLock.isHeldByCurrentThread()) {
                    redissonLock.unlock();
                }
            }
        }
    }

    public void redisRedLock() {
        RedissonRedLock redissonRedLock = new RedissonRedLock(redisson.getLock(Redis_Lock), redisson.getLock(Redis_Lock));
        redissonRedLock.lock();
    }

    public void eee() {
        String result = stringRedisTemplate.opsForValue().get("goods:001");
        if (StringUtils.isEmpty(result)) {
            RLock redissonLock = redisson.getLock(Redis_Lock);//非公平锁  Redis_Lock = "hello";
            redissonLock.lock();
            try {
                result = stringRedisTemplate.opsForValue().get("goods:001");
                if (StringUtils.isEmpty(result)) {
                    // 查库

                    // 放缓存
                    stringRedisTemplate.opsForValue().set("goods", "1234", 10L, TimeUnit.SECONDS);
                }
            }finally {
                if (redissonLock.isLocked()) {
                    if (redissonLock.isHeldByCurrentThread()) {
                        redissonLock.unlock();
                    }
                }
            }
        }
    }
}

